//
//  ComplaintFeedbackCell.m
//  PigMarket
//
//  Created by mac on 2019/3/23.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "ComplaintFeedbackCell.h"


@interface ComplaintFeedbackCell ()
/**
 selectBtn
 */
@property (nonatomic , strong) UIButton *selectBtn;

/**
 titleLab
 */
@property (nonatomic , strong) UILabel *titleLab;

@end
@implementation ComplaintFeedbackCell
+ (instancetype)cellWithTableView:(UITableView *)tableView {
    static NSString *identifierID = @"identifierID";
    ComplaintFeedbackCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierID];
    if (cell == nil) {
        cell = [[ComplaintFeedbackCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifierID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}
#pragma mark - init method
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initView];
        [self initLayout];
    }
    return self;
}

- (void)initView {
    // 添加子控件
    [self.contentView addSubview:self.selectBtn];
    [self.contentView addSubview:self.titleLab];
    [self.contentView addSubview:self.inputTextView];
    [self.contentView addSubview:self.photosView];
    [self.contentView addSubview:self.phoneField];
}

- (void)initLayout {
    // 布局子控件
    [self.selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(22, 22));
    }];
    
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.selectBtn.mas_right).offset(15);
        make.centerY.mas_equalTo(self.selectBtn.mas_centerY);
        make.width.mas_equalTo(SCREEN_WIDTH - 80);
    }];
    [self.inputTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.right.mas_equalTo(-20);
        make.left.mas_equalTo(20);
        make.height.mas_equalTo(120);
    }];
    [self.photosView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.bottom.mas_equalTo(-20);
        make.size.mas_equalTo(CGSizeMake(160, 74));
    }];
    [self.phoneField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.titleLab.mas_right).offset(20);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.right.mas_equalTo(-20);
    }];
}
- (void)selectBtnTapped {
    self.selectBtn.selected = !self.selectBtn.selected;

    if (self.selectQuestionBlock) {
        self.selectQuestionBlock (self.selectBtn.selected);
    }
}

#pragma mark - getter and setter
- (void)setComplaintFeedbackModel:(ComplaintFeedback *)complaintFeedbackModel {
    _complaintFeedbackModel = complaintFeedbackModel;
    if (![complaintFeedbackModel.dataKey isEqualToString:@""] ) {
        self.titleLab.text = complaintFeedbackModel.dataKey;
        self.titleLab.hidden = NO;
        self.selectBtn.hidden = NO;
        self.inputTextView.hidden = YES;
        self.photosView.hidden = YES;
        if ([complaintFeedbackModel.dataKey isEqualToString:@"手机号"]) {
            self.selectBtn.hidden = YES;
            self.phoneField.hidden = NO;
            [self.titleLab mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(20);
                make.centerY.mas_equalTo(self.contentView.mas_centerY);
                make.width.mas_equalTo(50);
            }];
        }else {
            self.phoneField.hidden = YES;
            self.selectBtn.hidden = NO;
        }
    }else {
        self.titleLab.hidden = YES;
        self.selectBtn.hidden = YES;
        self.inputTextView.hidden = NO;
        self.photosView.hidden = NO;
        self.phoneField.hidden = YES;
    }
}
- (UIButton *)selectBtn {
    if (!_selectBtn) {
        _selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_selectBtn setBackgroundImage:[UIImage imageNamed:@"mine_feedback"] forState:UIControlStateNormal];
        [_selectBtn setBackgroundImage:[UIImage imageNamed:@"mine_feedback_selected"] forState:UIControlStateSelected];
        [_selectBtn addTarget:self action:@selector(selectBtnTapped) forControlEvents:UIControlEventTouchUpInside];
    }
    return _selectBtn;
}

- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] init];
        _titleLab.textColor = [UIColor textBlackColor];
        _titleLab.font = [UIFont largerFont];
    }
    return _titleLab;
}
- (MyTextView *)inputTextView {
    if (!_inputTextView) {
        _inputTextView = [[MyTextView alloc] init];
        _inputTextView.placeholder = @"请输入问题反馈";
    }
    return _inputTextView;
}
- (UITextField *)phoneField {
    if (!_phoneField) {
        _phoneField = [[UITextField alloc] init];
        _phoneField.placeholder = @"请输入手机号";
        _phoneField.clearButtonMode = UITextFieldViewModeAlways;
    }
    return _phoneField;
}
- (SelectPhotosView *)photosView {
    if (!_photosView) {
        _photosView = [[SelectPhotosView alloc] init];
    }
    return _photosView;
}
@end
